package com.cljz.peipei.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.cljz.peipei.R;
import com.cljz.peipei.model.net.bean.TagListBean;
import com.clzj.peipei.bean.Tag;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by apple on 2017/4/20.
 */

public class HaveChosenAdapter extends RecyclerView.Adapter<HaveChosenAdapter.HaveChosenHolder> {
    private OnItemClickListener mOnItemClickListener;

    public List<Tag> myTagList;
    private Context context;

    public void addTag(Tag tag){
        if (tag == null) return;
        myTagList.add(tag);
        notifyDataSetChanged();
    }

    public void removeTag(Tag tag){
        if (tag == null) return;
        myTagList.remove(tag);
        notifyDataSetChanged();
    }

    public void setMyTagList(List<Tag> tags) {
        myTagList.clear();
        myTagList.addAll(tags);
        notifyDataSetChanged();
    }

    public HaveChosenAdapter(Context context) {
        this.context = context;
        myTagList = new ArrayList<>();
    }

    @Override
    public HaveChosenHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new HaveChosenHolder(View.inflate(context, R.layout.item_have_chosen, null));
    }

    @Override
    public void onBindViewHolder(HaveChosenHolder holder, final int position) {
        holder.setData(myTagList, position);
        if (mOnItemClickListener != null) {
            holder.itemView.findViewById(R.id.iv_delete).setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onClick(myTagList.get(position));
                }
            });

            holder.itemView.findViewById(R.id.iv_delete).setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    mOnItemClickListener.onLongClick(myTagList.get(position));
                    return false;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return myTagList == null ? 0 : myTagList.size();
    }

    public boolean contains(TagListBean.DataBean.tagBean tagBean) {
        for (Tag tag: myTagList) {
            if (tagBean.getId().equals(tag.getId())){
                return true;
            }
        }

        return false;
    }

    static class HaveChosenHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_tag)
        TextView tvTag;
        @BindView(R.id.iv_delete)
        ImageView ivDelete;

        public HaveChosenHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void setData(List<Tag> myTagList, int position) {
            String tag = myTagList.get(position).getTag_name();
            tvTag.setText(tag);
        }
    }

    public interface OnItemClickListener {
        void onClick(Tag dataBean);

        void onLongClick(Tag dataBean);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }
}
